package com.example.demo.controller;

import com.example.demo.bean.*;
import com.example.demo.service.*;
import com.example.demo.util.TransferUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Date;
import java.text.ParseException;
import java.util.*;

@RestController
public class DataController {

    @Autowired
    private DataProvider dataProvider;
    @Autowired
    private UserService userService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private ERecordService eRecordService;
    @Autowired
    private RiskAreaService riskAreaService;

    @RequestMapping("/data/china")
    public Map<String, Object> China() throws IOException {
        List<RegionStates> regionStates = dataProvider.getRegionStates("China");
        TransferUtil.transferToChinese(regionStates);
        Map<String,Object> map =new HashMap<String,Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", regionStates);
        return map;
    }

    @RequestMapping("/data/global")
    public Map<String, Object> global() throws IOException {
        List<RegionStates> num = dataProvider.getNum();
        Map<String,Object> map =new HashMap<String,Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", num);
        return map;
    }

    @RequestMapping("/data/regional")
    public Map<String, Object> regional() throws IOException {
        List<RegionStates> regionStates = dataProvider.getAllRegionStates();
        Map<String,Object> map =new HashMap<String,Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", regionStates);
        return map;
    }

    @RequestMapping("/data/country")
    public Map<String, Object> Country(String country) throws IOException {
        Map<String,Object> map =new HashMap<String,Object>();
        map.put("code", 0);
        map.put("msg", "");
        List<RegionStates> num = dataProvider.getNum();
        for(RegionStates res : num)
        {
            if(res.getCountry().equals(country))
            {
                LinkedList<RegionStates> regionStates = new LinkedList<>();
                regionStates.add(res);
                map.put("data",regionStates);
            }
        }
        map.putIfAbsent("data", null);
        return map;
    }

    @RequestMapping("/data/locality")
    public Map<String, Object> locality(String state) throws IOException {
        Map<String,Object> map =new HashMap<String,Object>();
        List<RegionStates> regionStates = dataProvider.getAllRegionStates();
        map.put("code", 0);
        map.put("msg", "");
        for (RegionStates regionState : regionStates) {
            if (regionState.getState().equals(state))
            {
                List<RegionStates> list = new LinkedList<>();
                list.add(regionState);
                map.put("data", list);
            }
        }
        map.putIfAbsent("data", null);
        return map;
    }

    @RequestMapping("/data/province")
    public Map<String, Object> province(String state) throws IOException {
        Map<String,Object> map =new HashMap<String,Object>();
        List<RegionStates> regionStates = dataProvider.getRegionStates("China");
        map.put("code", 0);
        map.put("msg", "");
        for (RegionStates regionState : regionStates) {
            if (regionState.getState().equals(state))
            {
                List<RegionStates> list = new LinkedList<>();
                list.add(regionState);
                map.put("data", list);
            }
        }
        map.putIfAbsent("data", null);
        return map;
    }

    @RequestMapping("/data/gorp")
    public Map<String,Object> getgorpNum() throws IOException {
        Map<String,Object> map =new HashMap<String,Object>();
        map.put("msg", "success");
        List<RegionStates> num = dataProvider.getNum();
        String[] name = new String[num.size()];
        int[] sum = new int[num.size()];
        int i = 0;
        for(RegionStates regionState : num)
        {
            name[i] = regionState.getCountry();
            sum[i] = regionState.getLatestTotalCases();
            i++;
        }
        map.put("country", name);
        map.put("num", sum);
        return map;
    }

    @RequestMapping("/reportrecord")
    public Map<String, Object> record() throws IOException {
        String username = "";
        if(SecurityContextHolder.getContext().getAuthentication().getPrincipal().getClass().toString().equals("class java.lang.String")){
            username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        }else {
            UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            username = principal.getUsername();
        }
        UserInfo userInfo = userInfoService.getUserInfoByUsername(username);
        List<ERecord> eRecords = eRecordService.getERcordByUfid(userInfo.getId());
        List<UserRecord> userRecords = new ArrayList<>();
        for (ERecord e:eRecords){
            UserRecord userRecord = new UserRecord(userInfo, e);
            userRecords.add(userRecord);
        }
        Map<String,Object> map =new HashMap<String,Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", userRecords);
        return map;
    }

    @RequestMapping("/selectUserInfoByCondition")
    public Map<String, Object> selectUserInfoByCondition(SearchUserRecordByCondition searchUserRecordByCondition) throws IOException{
        String username = "";
        if(SecurityContextHolder.getContext().getAuthentication().getPrincipal().getClass().toString().equals("class java.lang.String")){
            username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        }else {
            UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            username = principal.getUsername();
        }
        UserInfo userInfo = userInfoService.getUserInfoByUsername(username);
        Map<String,Object> map =new HashMap<String,Object>();
        map.put("code", 0);
        map.put("msg", "");
        List<ERecord> eRecords = eRecordService.getERecordByCondition(searchUserRecordByCondition);
        if(eRecords != null){
            List<UserRecord> userRecords = new ArrayList<>();
            for (ERecord e:eRecords){
                UserRecord userRecord = new UserRecord(userInfo, e);
                userRecords.add(userRecord);
            }
            map.put("data", userRecords);
        }else {
            map.put("data", null);
        }
        return map;
    }

    @RequestMapping("/mangeuser")
    public Map<String, Object> userMange()  throws IOException{
        List<UserInfo> userInfos = userInfoService.getAllUserInfo();
        List<UserRecord> userRecords = new ArrayList<>();
        for (UserInfo u:userInfos){
            List<ERecord> eRecords = eRecordService.getERcordByUfid(u.getId());
            ERecord eRecord = eRecords.get(eRecords.size()-1);
            UserRecord userRecord = new UserRecord(u, eRecord);
            java.sql.Date currentDate = new java.sql.Date(System.currentTimeMillis());
            if(eRecord.getTime().toString().equals(currentDate.toString())){
                userRecord.setReportstatus("已打卡");
            }else {
                userRecord.setReportstatus("未打卡");
            }
            userRecords.add(userRecord);
        }
        Map<String,Object> map =new HashMap<String,Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", userRecords);
        return map;
    }

    @RequestMapping("/userdetails")
    public Map<String, Object> userDetails(HttpSession session) throws IOException{
        Integer id = (Integer) session.getAttribute("id");
        List<ERecord> eRecords = eRecordService.getERcordByUfid(id);
        UserInfo userInfo = userInfoService.getAllUserInfo().get(0);
        List<UserRecord> userRecords = new ArrayList<>();
        for (ERecord e:eRecords){
            UserRecord userRecord = new UserRecord(userInfo, e);
            userRecords.add(userRecord);
        }
        Map<String,Object> map =new HashMap<String,Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", userRecords);
        return map;
    }

    @RequestMapping("/selectUserByCondition")
    public Map<String, Object> selectUserByCondition(SearchUserByCondition searchUserByCondition) throws IOException{
        Map<String,Object> map =new HashMap<String,Object>();
        map.put("code", 0);
        map.put("msg", "");
        List<UserInfo> userInfos = userInfoService.getUserInfoByCondition(searchUserByCondition);
        if(userInfos != null){
            List<UserRecord> userRecords = new ArrayList<>();
            for (UserInfo u:userInfos){
                List<ERecord> eRecords = eRecordService.getERcordByUfid(u.getId());
                ERecord eRecord = eRecords.get(eRecords.size()-1);
                UserRecord userRecord = new UserRecord(u, eRecord);
                java.sql.Date currentDate = new java.sql.Date(System.currentTimeMillis());
                if(eRecord.getTime().toString().equals(currentDate.toString())){
                    userRecord.setReportstatus("已打卡");
                }else {
                    userRecord.setReportstatus("未打卡");
                }
                userRecords.add(userRecord);
            }
            map.put("data", userRecords);
        }else {
            map.put("data", null);
        }
        return map;
    }

    @RequestMapping("/selectUserRecordByCondition")
    public Map<String, Object> selectUserRecordByCondition(SearchUserRecordByCondition searchUserRecordByCondition) throws IOException, ParseException {
        Map<String,Object> map =new HashMap<String,Object>();
        map.put("code", 0);
        map.put("msg", "");
        List<ERecord> eRecords = eRecordService.getERecordByCondition(searchUserRecordByCondition);
        if(eRecords != null){
            UserInfo userInfo = userInfoService.getAllUserInfo().get(0);
            List<UserRecord> userRecords = new ArrayList<>();
            for (ERecord e:eRecords){
                UserRecord userRecord = new UserRecord(userInfo, e);
                userRecords.add(userRecord);
            }
            map.put("data", userRecords);
        }else {
            map.put("data", null);
        }
        return map;
    }

    @RequestMapping("/usernotreport")
    public Map<String, Object> usernotreport() throws IOException{
        List<UserInfo> userInfos = userInfoService.getAllUserInfo();
        List<UserRecord> userRecords = new ArrayList<>();
        for (UserInfo u:userInfos){
            List<ERecord> eRecords = eRecordService.getERcordByUfid(u.getId());
            ERecord eRecord = eRecords.get(eRecords.size()-1);
            UserRecord userRecord = new UserRecord(u, eRecord);
            java.sql.Date currentDate = new java.sql.Date(System.currentTimeMillis());
            if(eRecord.getTime().toString().equals(currentDate.toString())){
                userRecord.setReportstatus("已打卡");
            }else {
                userRecord.setReportstatus("未打卡");
                userRecords.add(userRecord);
            }
        }
        Map<String,Object> map =new HashMap<String,Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", userRecords);
        return map;
    }

    @RequestMapping("/usernumbermange")
    public Map<String, Object> usernumbermange() throws IOException{
        List<User> users = userService.getAllComUser();
        Map<String,Object> map =new HashMap<String,Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", users);
        return map;
    }

    @RequestMapping("/selectUserNumberByCondition")
    public Map<String, Object> selectUserNumberByCondition(SearchUserNumberByCondition searchUserNumberByCondition) throws IOException{
        List<User> users = userService.selectUserByCondition(searchUserNumberByCondition);
        Map<String,Object> map =new HashMap<String,Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", users);
        return map;
    }

    @RequestMapping("/riskmange")
    public Map<String, Object> riskmange() throws IOException{
        List<RiskArea> riskAreas = riskAreaService.getAllRiskArea();
        Map<String,Object> map =new HashMap<String,Object>();
        map.put("code", 0);
        map.put("msg", "hao");
        map.put("data", riskAreas);
        return map;
    }

    @RequestMapping("/addriskarea")
    public Map<String, Object> addriskarea(String highriskarea, String midriskarea) throws IOException{
        RiskArea riskArea = new RiskArea(null, highriskarea, midriskarea);
        riskAreaService.addRiskArea(riskArea);
        List<RiskArea> riskAreas = riskAreaService.getAllRiskArea();
        Map<String,Object> map =new HashMap<String,Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", riskAreas);
        return map;
    }

    @GetMapping("/deleteusernumber")
    public void deleteusernumber(@RequestParam( "id" ) Integer id ){
        userService.deleteUser(id);
    }

    @GetMapping( "/deleteriskarea" )
    public void deleteriskarea(@RequestParam( "id" ) Integer id ){
        riskAreaService.deleteRiskArea(id);
    }

    @RequestMapping("/adminmange")
    public Map<String, Object> adminmange() throws IOException{
        List<User> admins = userService.getAllAdmin();
        Map<String,Object> map =new HashMap<String,Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", admins);
        return map;
    }

    @RequestMapping("/addadmin")
    public Map<String, Object> addadmin(String username, String password) throws IOException{
        User user = new User(null, username, password, "管理员", 0);
        userService.addUser(user);
        List<User> admins = userService.getAllAdmin();
        Map<String,Object> map =new HashMap<String,Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", admins);
        return map;
    }
}
